<?php
namespace App\Http\Controllers\Admin\Member;

use App\Repositories\Admin\CompanyRepository;
use App\Utilities\JiugeForm;
use App\Utilities\Upload;
use Illuminate\Http\Request;

class CompanyController extends BaseController
{
    /**
     * 员工
     */

    protected $view = 'admin.member.company.';//视图前缀
    protected $url = 'company';//路由前缀
    protected $model;
//    protected $formElementArr = [//表单元素
//        //array(表单选项,中文名称,name字段名称,表单提示,js验证规则)
//        array(1,'用户名','name','用户名',1),
//        array(5,'所属父级','pid','父级分类',6),
//    ];
//    protected $fieldShowArr = [
//        'id' => 'ID',
//        'cateName' => '企业类型',
//        'legalName' => '法人名称',
//        'legalSexName' => '法人性别',
//        'legalMobile' => '法人手机号码',
//        'legalIdcard' => '法人身份证号码',
//        'idcardImg1' => '法人身份证正面',
//        'idcardImg2' => '法人身份证背面',
//        'idcardImg3' => '法人身份证手持正面',
//        'comName' => '公司名称',
//        'comTel' => '公司座机',
//        'comFax' => '公司传真',
//        'comAddress' => '公司地址',
//        'comCityName' => '省市区',
//        'cusLike' => '客户满意度',
//        'quality' => '品质合格率',
//        'pact' => '合同履行率',
//        'outType' => '业务类型',
//        'ageRange' => '年龄段几岁到几岁',
//        'detail' => '公司简介',
//    ];

    public function __construct()
    {
        parent::__construct();
        $this->prefix_url = $this->prefix_url.'/'.$this->url;
        $this->model = new CompanyRepository();
        view()->share('model',$this->model);
        view()->share('prefix_url',$this->prefix_url);
        view()->share('jiugeForm',new JiugeForm($this->prefix_url));
    }

    public function index()
    {
        $models = $this->model->getIndexList();
        return view($this->view.'index',array(
            'datas' => $models,
            'model' => $this->model,
        ));
    }

    public function create(Request $request)
    {
        $uid = isset($request->uid) ? $request->uid : 0;
        return view($this->view.'edit',array(
            'outTypes' => $this->model->getCatesByPid(108),
            'uid' => $uid,
        ));
    }

    public function store(Request $request)
    {
        $resData = $this->getData($request);
        $this->model->setAdd($resData);
        return redirect($this->prefix_url);
    }

    public function edit(Request $request,$id)
    {
        $uid = isset($request->uid) ? $request->uid : 0;
        $model = $this->model->getAdminShow($id);
        if (!$uid) { $uid = $model->uid; }
        return view($this->view.'edit',array(
            'outTypes' => $this->model->getCatesByPid(108),
            'data' => $model,
            'uid' => $uid,
        ));
    }

    public function update(Request $request,$id)
    {
        $model = $this->model->getAdminShow($id);
        $resData = $this->getData($request);
        $resImgs = explode('|',$resData['legal_idcard_imgs']);
        if ($model->idcardImg0 && $resImgs[0]) {
            $oldImg0 = $model->idcardImg0;
        }
        if ($model->idcardImg1 && $resImgs[1]) {
            $oldImg1 = $model->idcardImg1;
        }
        if ($model->idcardImg2 && $resImgs[2]) {
            $oldImg2 = $model->idcardImg2;
        }
        $this->model->setModify($resData,$resData['uid']);
        if (isset($oldImg0) && file_exists(ltrim($oldImg0,'/'))) {
            unlink(ltrim($oldImg0,'/'));
        }
        if (isset($oldImg1) && file_exists(ltrim($oldImg1,'/'))) {
            unlink(ltrim($oldImg1,'/'));
        }
        if (isset($oldImg2) && file_exists(ltrim($oldImg2,'/'))) {
            unlink(ltrim($oldImg2,'/'));
        }
        return redirect($this->prefix_url);
    }

    public function show($id)
    {
        $model = $this->model->getAdminShow($id);
        return view($this->view.'show',array(
            'data' => $model,
        ));
    }

    /**
     * 收集、验证
     */
    public function getData(Request $request)
    {
        $res = $request->all();
        $resData = array();
        foreach ($res as $key => $re) {
            $resData[$key] = $re ? $re : 0;
        }
        if (isset($res['out_types'])) { //业务类型
            $resData['out_type'] = $res['out_types'] ?
                implode('||',$res['out_types']) : '';
        } else {
            $resData['out_types'] = '';
        }
        $ageStart = $resData['ageStart'] ? $resData['ageStart'] : 0;
        $ageEnd = $resData['ageEnd'] ? $resData['ageEnd'] : 0;
        $resData['age_range'] = implode(',',[$ageStart,$ageEnd]);
        //个别字段验证
        $fields = [
            'legal_name','legal_sex','legal_mobile','legal_idcard',
            'com_name','com_city','cate',
        ];
        foreach ($fields as $field) {
            if (!$resData[$field]) {
                echo "<script>alert('信息不全！');history.go(-1);</script>"; exit;
            }
        }
        //图片处理
        if (isset($request->idcardImg0)) {
            $resImg0 = Upload::uploadImg($request,'idcardImg0');
            if ($resImg0['code']==0) {
                echo "<script>alert('正面照上传错误！');history.go(-1);</script>"; exit;
            } else {
                $img0 = $resImg0['filePath'];
            }
        } else {
            $img0 = '0';
        }
        if (isset($request->idcardImg1)) {
            $resImg1 = Upload::uploadImg($request,'idcardImg1');
            if ($resImg1['code']==0) {
                echo "<script>alert('侧面照上传错误！');history.go(-1);</script>"; exit;
            } else {
                $img1 = $resImg1['filePath'];
            }
        } else {
            $img1 = '0';
        }
        if (isset($request->idcardImg2)) {
            $resImg2 = Upload::uploadImg($request,'idcardImg2');
            if ($resImg2['code']==0) {
                echo "<script>alert('手持正面照上传错误！');history.go(-1);</script>"; exit;
            } else {
                $img2 = $resImg2['filePath'];
            }
        } else {
            $img2 = '0';
        };
        $resData['legal_idcard_imgs'] = implode('|',[$img0,$img1,$img2]);
        //动态参数组合
        if ($resData['cate']==1) {
            $param1 = [
                'output' => $resData['output'],
                'truck' => $resData['truck'],
                'piece' => $resData['piece'],
                'bale' => $resData['bale'],
                'single' => $resData['single'],
                'cut' => $resData['cut'],
                'back' => $resData['back'],
                'total' => $resData['total'],
            ];
            $resData['param'] = json_encode($param1);
            $resData['param2'] = '';
            foreach ($param1 as $key => $item) {
                unset($resData[$key]);
            }
        } else if ($resData['cate']==2) {
            $param2 = [
                'scale' => $resData['scale'],
                'totalSale' => $resData['totalSale'],
                'yearSale' => $resData['yearSale'],
                'truck' => $resData['truck'],
                'brand' => $resData['brand'],
            ];
            $resData['param'] = '';
            $resData['param2'] = json_encode($param2);
            foreach ($param2 as $key => $item) {
                unset($resData[$key]);
            }
        } else {
            $param2 = [
                'output' => $resData['output'],
                'truck' => $resData['truck'],
                'piece' => $resData['piece'],
                'bale' => $resData['bale'],
                'single' => $resData['single'],
                'cut' => $resData['cut'],
                'back' => $resData['back'],
                'total' => $resData['total'],
                'scale' => $resData['scale'],
                'totalSale' => $resData['totalSale'],
                'yearSale' => $resData['yearSale'],
                'brand' => $resData['brand'],
            ];
            $resData['param'] = '';
            $resData['param2'] = json_encode($param2);
            foreach ($param2 as $key => $item) {
                unset($resData[$key]);
            }
        }
        unset($resData['_token']); unset($resData['_method']);
        unset($resData['ageStart']); unset($resData['ageEnd']);
        unset($resData['out_types']);
        return $resData;
    }

    /**
     * 通过父级PID，获取业务类型列表
     */
    public function getCatesByPid(Request $request)
    {
        if ($request->ajax()) {
            $cate = $request->cate;
            if ($cate==1) {
                $resData = $this->model->getCatesByPid(108);
            } else {
                $resData = $this->model->getCatesByPid(109);
            }
            if (!$resData) {
                return array('code'=>-2,'msg'=>'没有数据！');
            }
            return array('code'=>0,'msg'=>'操作成功！','data'=>$resData);
        }
        return array('code'=>-1,'msg'=>'操作失败！');
    }

    /**
     * 通过父级PID，获取省市区列表
     */
    public function getCitysByPid(Request $request)
    {
        if ($request->ajax()) {
            $pid = $request->pid;
            $level = $request->level;
            $resData = $this->model->getCitys($pid,$level);
            if (!$resData) {
                return array('code'=>-2,'msg'=>'没有数据！');
            }
            return array('code'=>0,'msg'=>'操作成功！','data'=>$resData);
        }
        return array('code'=>-1,'msg'=>'操作失败！');
    }

    public function getModelById($id)
    {
        $model = $this->model->getAdminShow($id);
        if (!$model) {
            echo "<script>alert('记录不存在！');history.go(-1);</script>"; exit;
        }
        return $model;
    }

    /**
     * ----------------------------
     * 可选信息
     * ----------------------------
     */

    /**
     * 可选信息编辑
     */
    public function editOption(Request $request)
    {
        $uid = isset($request->uid) ? $request->uid : 0;
        $cate = $request->cate;
        $options = $this->model->getAdminOptionByUid($uid,$cate);
//        dd($options);
        return view($this->view.'option',array(
            'legals' => $options['legals'],
            'coms' => $options['coms'],
            'products' => $options['products'],
            'styles' => $options['styles'],
            'proTypes' => $options['proTypes'],
            'designs' => $options['designs'],
            'equips' => $options['equips'],
            'departs' => $options['departs'],
            'dayOutPuts' => $options['dayOutPuts'],
            'requirePros' => $options['requirePros'],
            'requireDeparts' => $options['requireDeparts'],
            'standards' => $options['standards'],
            'facEnvs' => $options['facEnvs'],
            'comEnvs' => $options['comEnvs'],
            'cate' => $cate,
            'uid' => $uid,
        ));
    }

    /**
     * 可选信息，设置法人证件
     */
    public function setOptionLegal(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $fileId = $request->fileLegalId;
        $fileKey = $request->fileLegalKey;
        $prefix = $request->prefix;
        if (!$fileId || !$fileKey) {
            echo "<script>alert('参数错误！');history.go(-1);</script>"; exit;
        }
        if (!isset($request->fileLegalUrl)) {
            echo "<script>alert('图片没有上传！');history.go(-1);</script>"; exit;
        }
        $resFile = Upload::uploadImg($request,'fileLegalUrl');
        if ($resFile['code']!=0) {
            echo "<script>alert('上传错误！');history.go(-1);</script>"; exit;
        }
        $imgUrl = $resFile['filePath'];
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['legals'][$fileKey][] = $imgUrl;
        $lastId = $this->model
            ->setAdminOptionByUid($uid,$model,$cate);
        return redirect($prefix.'#file');
    }

    /**
     * 可选信息，设置公司证件
     */
    public function setOptionCom(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $fileId = $request->fileComId;
        $fileKey = $request->fileComKey;
        $prefix = $request->prefix;
        if (!$fileId || !$fileKey) {
            echo "<script>alert('参数错误！');history.go(-1);</script>"; exit;
        }
        if (!isset($request->fileComUrl)) {
            echo "<script>alert('图片没有上传！');history.go(-1);</script>"; exit;
        }
        $resFile = Upload::uploadImg($request,'fileComUrl');
        if ($resFile['code']!=0) {
            echo "<script>alert('上传错误！');history.go(-1);</script>"; exit;
        }
        $imgUrl = $resFile['filePath'];
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['coms'][$fileKey][] = $imgUrl;
        $lastId = $this->model
            ->setAdminOptionByUid($uid,$model,$cate);
        return redirect($prefix.'#file');
    }

    /**
     * 可选信息，获取产品
     */
    public function getOptionPros(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $options = $this->model->getAdminOptionByUid($uid,$cate);
        $pros = $options['pros'];
//        $products = $options['products'];
        $zhenArr = array();
        $suoArr = array();
        $baoArr = array();
        if (!$pros) {
            $zhens = $this->model->getCatesByPid(19); //针织类
            $suos = $this->model->getCatesByPid(20); //梭织类
            $baos = $this->model->getCatesByPid(21); //箱包类
            foreach ($zhens as $key => $zhen) {
                $zhenArr[$key][0] = $zhen['id'];
                $zhenArr[$key][4] = $zhen['name'];
                $zhenArr[$key][1] = 0;
                $zhenArr[$key][2] = 0;
                $zhenArr[$key][3] = 0;
            }
            foreach ($suos as $key => $suo) {
                $suoArr[$key][0] = $suo['id'];
                $suoArr[$key][4] = $suo['name'];
                $suoArr[$key][1] = 0;
                $suoArr[$key][2] = 0;
                $suoArr[$key][3] = 0;
            }
            foreach ($baos as $key => $bao) {
                $baoArr[$key][0] = $bao['id'];
                $baoArr[$key][4] = $bao['name'];
                $baoArr[$key][1] = 0;
                $baoArr[$key][2] = 0;
                $baoArr[$key][3] = 0;
            }
        } else {
            foreach ($pros['zhens'] as $key => $zhen) {
                $zhenArr[$key][0] = $zhen[0];
                $zhenArr[$key][4] = $this->model->getCateNameById($zhen[0]);
                $zhenArr[$key][1] = $zhen[1];
                $zhenArr[$key][2] = $zhen[2];
                $zhenArr[$key][3] = $zhen[3];
            }
            foreach ($pros['suos'] as $key => $suo) {
                $suoArr[$key][0] = $suo[0];
                $suoArr[$key][4] = $this->model->getCateNameById($suo[0]);
                $suoArr[$key][1] = $suo[1];
                $suoArr[$key][2] = $suo[2];
                $suoArr[$key][3] = $suo[3];
            }
            foreach ($pros['baos'] as $key => $bao) {
                $baoArr[$key][0] = $bao[0];
                $baoArr[$key][4] = $this->model->getCateNameById($bao[0]);
                $baoArr[$key][1] = $bao[1];
                $baoArr[$key][2] = $bao[2];
                $baoArr[$key][3] = $bao[3];
            }
        }
        return view($this->view.'option.product',array(
            'zhens' => $zhenArr,
            'suos' => $suoArr,
            'baos' => $baoArr,
            'uid' => $uid,
            'cate' => $cate,
        ));
    }

    /**
     * 可选信息，设置公司产品
     */
    public function setOptionPros(Request $request)
    {
        $res = $request->all();
        $uid = $res['uid'];
        $cate = $res['cate'];
        $zhenIds = $res['zhenIds'];
        $suoIds = $res['suoIds'];
        $baoIds = $res['baoIds'];
        $zhens = array();
        $suos = array();
        $baos = array();
        foreach ($zhenIds as $key => $zhenId) {
            $zhens[$key][0] = $zhenId;
            $zhens[$key][1] = isset($res['males'.$zhenId]) ? 1 : 0;
            $zhens[$key][2] = isset($res['females'.$zhenId]) ? 1 : 0;
            $zhens[$key][3] = isset($res['childs'.$zhenId]) ? 1 : 0;
        }
        foreach ($suoIds as $key => $suoId) {
            $suos[$key][0] = $suoId;
            $suos[$key][1] = isset($res['males'.$suoId]) ? 1 : 0;
            $suos[$key][2] = isset($res['females'.$suoId]) ? 1 : 0;
            $suos[$key][3] = isset($res['childs'.$suoId]) ? 1 : 0;
        }
        foreach ($baoIds as $key => $baoId) {
            $baos[$key][0] = $baoId;
            $baos[$key][1] = isset($res['males'.$baoId]) ? 1 : 0;
            $baos[$key][2] = isset($res['females'.$baoId]) ? 1 : 0;
            $baos[$key][3] = isset($res['childs'.$baoId]) ? 1 : 0;
        }
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['products'] = array(
            'zhens' => $zhens,
            'suos' => $suos,
            'baos' => $baos,
        );
        $model['pros'] = $model['products'];
        $this->model->setAdminOptionByUid($uid,$model,$cate);
        $prefix = $this->prefix_url.'/option-get';
        $param = '?uid='.$uid.'&cate='.$cate.'#product';
        return redirect($prefix.$param);
    }

    /**
     * 可选信息，获取产品风格
     */
    public function getOptionStyles(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $options = $this->model->getAdminOptionByUid($uid,$cate);
        $styleArr = array();
        if (!$options['styles']) {
            $resStyles = $this->model->getCatesByPid(146);
            foreach ($resStyles as $key => $resStyle) {
                $styleArr[$key][0] = $resStyle['id'];
                $styleArr[$key][2] = $resStyle['name'];
                $styleArr[$key][1] = 0;
            }
        } else {
            foreach ($options['styles'] as $key => $style) {
                $styleArr[$key][0] = $style['id'];
                $styleArr[$key][2] = $style['name'];
                $styleArr[$key][1] = $style['isExist'];
            }
        }
        return view($this->view.'option.style',array(
            'styles' => $styleArr,
            'uid' => $uid,
            'cate' => $cate,
        ));
    }

    /**
     * 可选信息，设置产品风格
     */
    public function setOptionStyles(Request $request)
    {
        $res = $request->all();
        $uid = $res['uid'];
        $cate = $res['cate'];
        $styleIds = $res['styleIds'];
        $styles = array();
        foreach ($styleIds as $key => $styleId) {
            $styles[$key][0] = $styleId;
            $styles[$key][1] = isset($res['styles'.$styleId]) ? 1 : 0;
        }
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['styles'] = $styles;
        $this->model->setAdminOptionByUid($uid,$model,$cate);
        $prefix = $this->prefix_url.'/option-get';
        $param = '?uid='.$uid.'&cate='.$cate.'#style';
        return redirect($prefix.$param);
    }

    /**
     * 可选信息，获取产品种类
     */
    public function getOptionProTypes(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $options = $this->model->getAdminOptionByUid($uid,$cate);
        return view($this->view.'option.proType',array(
            'proTypes' => $options['proTypes'],
            'uid' => $uid,
            'cate' => $cate,
        ));
    }

    /**
     * 可选信息，设置产品种类
     */
    public function setOptionProTypes(Request $request)
    {
        $res = $request->all();
        $uid = $res['uid'];
        $cate = $res['cate'];
        $keys = (isset($res['keys'])&&$res['keys']) ? $res['keys'] : array();
        $proTypes = array();
        //老数据编辑
        if ($keys) {
            foreach ($keys as $key) {
                $cate = $res['cate'.$key];
                $name = $res['name'.$key];
                $male = $res['male'.$key];
                $female = $res['female'.$key];
                $child = $res['child'.$key];
                if (!$cate || !$name || !$male || !$female || !$child) {
                    echo "<script>alert('编辑数据不全！');history.go(-1);</script>"; exit;
                }
                $proTypes[$key]['cate'] = $cate;
                $proTypes[$key]['name'] = $name;
                $proTypes[$key]['male'] = $male;
                $proTypes[$key]['female'] = $female;
                $proTypes[$key]['child'] = $child;
            }
        }
        //新数据增加
        $add1 = $res['cate'] ? $res['cate'] : 0;
        $add2 = $res['name'] ? $res['name'] : 0;
        $add3 = $res['male'] ? $res['male'] : 0;
        $add4 = $res['female'] ? $res['female'] : 0;
        $add5 = $res['child'] ? $res['child'] : 0;
        if (($add1 && (!$add2||!$add3||!$add4||!$add5)) ||
            ($add2 && (!$add1||!$add3||!$add4||!$add5)) ||
            ($add3 && (!$add2||!$add1||!$add4||!$add5)) ||
            ($add4 && (!$add2||!$add3||!$add1||!$add5)) ||
            ($add5 && (!$add2||!$add3||!$add4||!$add1))) {
            echo "<script>alert('新增数据不全！');history.go(-1);</script>"; exit;
        }
        $proTypes[] = array(
            'cate' => $add1,
            'name' => $add2,
            'male' => $add3,
            'female' => $add4,
            'child' => $add5,
        );
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['proTypes'] = $proTypes;
        $this->model->setAdminOptionByUid($uid,$model,$cate);
        $prefix = $this->prefix_url.'/option-get';
        $param = '?uid='.$uid.'&cate='.$cate.'#proType';
        return redirect($prefix.$param);
    }

    /**
     * 可选信息，获取款式数量
     */
    public function getOptionDesigns(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $options = $this->model->getAdminOptionByUid($uid,$cate);
        return view($this->view.'option.design',array(
            'designs' => $options['designs'],
            'uid' => $uid,
            'cate' => $cate,
        ));
    }

    /**
     * 可选信息，设置款式数量
     */
    public function setOptionDesigns(Request $request)
    {
        $res = $request->all();
        $uid = $res['uid'];
        $cate = $res['cate'];
        $keys = (isset($res['keys'])&&$res['keys']) ? $res['keys'] : array();
        $designs = array();
        //老数据编辑
        if ($keys) {
            foreach ($keys as $key) {
                $name = $res['name'.$key];
                $num = $res['num'.$key];
                if (!$name || !$num) {
                    echo "<script>alert('编辑数据不全！');history.go(-1);</script>"; exit;
                }
                $designs[$key]['name'] = $name;
                $designs[$key]['num'] = $num;
            }
        }
        //新数据增加
        $add1 = $res['name'] ? $res['name'] : 0;
        $add2 = $res['num'] ? $res['num'] : 0;
        if (($add1 && !$add2) || (!$add1 && $add2)) {
            echo "<script>alert('新增数据不全！');history.go(-1);</script>"; exit;
        }
        $designs[] = array(
            'name' => $add1,
            'num' => $add2,
        );
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['designs'] = $designs;
        $this->model->setAdminOptionByUid($uid,$model,$cate);
        $prefix = $this->prefix_url.'/option-get';
        $param = '?uid='.$uid.'&cate='.$cate.'#design';
        return redirect($prefix.$param);
    }

    /**
     * 可选信息，获取工程设备
     */
    public function getOptionEquips(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $options = $this->model->getAdminOptionByUid($uid,$cate);
        return view($this->view.'option.equip',array(
            'equips' => $options['equips'],
            'uid' => $uid,
            'cate' => $cate,
        ));
    }

    /**
     * 可选信息，设置工厂设备
     */
    public function setOptionEquips(Request $request)
    {
        $res = $request->all();
        $uid = $res['uid'];
        $cate = $res['cate'];
        $keys = (isset($res['keys'])&&$res['keys']) ? $res['keys'] : array();
        $equips = array();
        //老数据编辑
        if ($keys) {
            foreach ($keys as $key) {
                $type = $res['type'.$key];
                $brand = $res['brand'.$key];
                $year = $res['year'.$key];
                $num = $res['num'.$key];
                if (!$type || !$brand || !$year || !$num) {
                    echo "<script>alert('编辑数据不全！');history.go(-1);</script>"; exit;
                }
                if (!isset($res['img'.$key])) {
                    $img = '';
                } else {
                    $resFile = Upload::uploadImg($request,'img'.$key);
                    if ($resFile['code']!=0) {
                        $img = '';
                    } else {
                        $img = $resFile['filePath'];
                    }
                }
                $equips[$key] = array($type,$brand,$year,$img,$num);
            }
        }
        //新数据增加
        $add1 = $res['type'] ? $res['type'] : 0;
        $add2 = $res['brand'] ? $res['brand'] : 0;
        $add3 = $res['year'] ? $res['year'] : 0;
        $add5 = $res['num'] ? $res['num'] : 0;
        if (($add1 && !$add2 && !$add3 && !$add5) ||
            (!$add1 && $add2 && !$add3 && !$add5) ||
            (!$add1 && !$add2 && $add3 && !$add5) ||
            (!$add1 && !$add2 && !$add3 && $add5)) {
            echo "<script>alert('新增数据不全！');history.go(-1);</script>"; exit;
        }
        if (!isset($res['img'])) {
            $add4 = '';
        } else {
            $resFile = Upload::uploadImg($request,'img');
            if ($resFile['code']!=0) {
                $add4 = '';
            } else {
                $add4 = $resFile['filePath'];
            }
        }
        $designs[] = array($add1,$add2,$add3,$add4,$add5);
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['equips'] = $designs;
        $this->model->setAdminOptionByUid($uid,$model,$cate);
        $prefix = $this->prefix_url.'/option-get';
        $param = '?uid='.$uid.'&cate='.$cate.'#equip';
        return redirect($prefix.$param);
    }

    /**
     * 可选信息，获取产量产能
     */
    public function getOptionYields(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $options = $this->model->getAdminOptionByUid($uid,$cate);
//        dd($options);
        return view($this->view.'option.yield',array(
            'departs' => $options['departs'],
            'dayOutPuts' => $options['dayOutPuts'],
            'pros' => $options['requirePros'],
            'depart2s' => $options['requireDeparts'],
            'standards' => $options['standards'],
            'uid' => $uid,
            'cate' => $cate,
        ));
    }

    /**
     * 可选信息，设置产量产能
     */
    public function setOptionYields(Request $request)
    {
        $res = $request->all();
        $uid = $res['uid'];
        $cate = $res['cate'];
        //部门人数
        $departKeys = (isset($res['departKeys'])&&$res['departKeys']) ?
            $res['departKeys'] : array();
        $departs = array();
        if ($departKeys) { //老数据编辑
            foreach ($departKeys as $key) {
                $departKey = $res['departKey'.$key];
                $departNum = $res['departNum'.$key];
                if (!$departKey || !$departNum) {
                    echo "<script>alert('编辑1数据不全！');history.go(-1);</script>"; exit;
                }
                $departs[$key] = array(
                    'key' => $departKey,
                    'num' => $departNum,
                );
            }
        }
        //----新数据增加
        $departAdd1 = $res['departKey'] ? $res['departKey'] : 0;
        $departAdd2 = $res['departNum'] ? $res['departNum'] : 0;
        if ($departAdd1 && $departAdd2) {
            if (($departAdd1 && !$departAdd2) || (!$departAdd1 && $departAdd2)) {
                echo "<script>alert('新增1数据不全！');history.go(-1);</script>"; exit;
            }
            $departs[] = array(
                'key' => $departAdd1,
                'num' => $departAdd2,
            );
        }
        //日产量
        $dayStart = $res['dayStart'] ? $res['dayStart'] : 0;
        $dayEnd = $res['dayEnd'] ? $res['dayEnd'] : 0;
        $dayOutPuts = array($dayStart,$dayEnd);
        //质量要求
        $proKeys = (isset($res['proKeys'])&&$res['proKeys']) ?
            $res['proKeys'] : array();
        $pros = array();
        if ($proKeys) { //老数据编辑
            foreach ($proKeys as $key) {
                $proKey = $res['proKey'.$key];
                $proVal = isset($res['proVal'.$key]) ? $res['proVal'.$key] : 0;
                if (!$proKey || !$proVal) {
                    echo "<script>alert('编辑3数据不全！');history.go(-1);</script>"; exit;
                }
                $pros[$key] = array(
                    'key' => $proKey,
                    'val' => $proVal,
                );
            }
        }
        //----新数据增加
        $pro1 = $res['proKey'] ? $res['proKey'] : 0;
        $pro2 = isset($res['proVal']) ? $res['proVal'] : 0;
        if ($pro1 && $pro2) {
            if (($pro1 && !$pro2) || (!$pro1 && $pro2)) {
                echo "<script>alert('新增3数据不全！');history.go(-1);</script>"; exit;
            }
            $pros[] = array(
                'key' => $pro1,
                'val' => $pro2,
            );
        }
        //质量部门
        $depart2Keys = (isset($res['depart2Keys'])&&$res['depart2Keys']) ?
            $res['depart2Keys'] : array();
        $depart2s = array();
        if ($depart2Keys) { //老数据编辑
            foreach ($depart2Keys as $key) {
                $depart2Key = $res['depart2Key'.$key];
                $depart2Num = $res['depart2Num'.$key];
                if (!$depart2Key || !$depart2Num) {
                    echo "<script>alert('编辑4数据不全！');history.go(-1);</script>"; exit;
                }
                $depart2s[$key] = array(
                    'key' => $depart2Key,
                    'num' => $depart2Num,
                );
            }
        }
        //----新数据增加
        $requireDepart1 = $res['depart2Key'] ? $res['depart2Key'] : 0;
        $requireDepart2 = $res['depart2Num'] ? $res['depart2Num'] : 0;
        if ($requireDepart1 && $requireDepart2) {
            if (($requireDepart1 && !$requireDepart2) ||
                (!$requireDepart1 && $requireDepart2)) {
                echo "<script>alert('新增3数据不全！');history.go(-1);</script>"; exit;
            }
            $depart2s[] = array(
                'key' => $requireDepart1,
                'num' => $requireDepart2,
            );
        }
        //质量部门
        $standard1 = isset($res['standard1']) ? 1 : 0;
        $standard2 = isset($res['standard2']) ? 1 : 0;
        $standard3 = isset($res['standard3']) ? 1 : 0;
        $standard4 = isset($res['standard4']) ? 1 : 0;
        $standard5 = isset($res['standard5']) ? 1 : 0;
        $standards = array($standard1,$standard2,$standard3,$standard4,$standard5);
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['departs'] = $departs;
        $model['dayOutPuts'] = $dayOutPuts;
        $model['requirePros'] = $pros;
        $model['requireDeparts'] = $depart2s;
        $model['standards'] = $standards;
//        dd($model);
        $this->model->setAdminOptionByUid($uid,$model,$cate);
        $prefix = $this->prefix_url.'/option-get';
        $param = '?uid='.$uid.'&cate='.$cate.'#yield';
        return redirect($prefix.$param);
    }

    /**
     * 可选信息，设置工厂环境
     */
    public function setOptionFacEnvs(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $envId = $request->envFacId;
        $envKey = $request->envFacKey;
        $prefix = $request->prefix;
        if (!$envId || !$envKey) {
            echo "<script>alert('参数错误！');history.go(-1);</script>"; exit;
        }
        if (!isset($request->envFacUrl)) {
            echo "<script>alert('图片没有上传！');history.go(-1);</script>"; exit;
        }
        $resFile = Upload::uploadImg($request,'envFacUrl');
        if ($resFile['code']!=0) {
            echo "<script>alert('上传错误！');history.go(-1);</script>"; exit;
        }
        $imgUrl = $resFile['filePath'];
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['facEnvs'][$envKey][] = $imgUrl;
//        dd($model,$cate);
        $lastId = $this->model
            ->setAdminOptionByUid($uid,$model,$cate);
        return redirect($prefix.'#facEnv');
    }

    /**
     * 可选信息，设置工厂环境
     */
    public function setOptionComEnvs(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $envId = $request->envComId;
        $envKey = $request->envComKey;
        $prefix = $request->prefix;
        if (!$envId || !$envKey) {
            echo "<script>alert('参数错误！');history.go(-1);</script>"; exit;
        }
        if (!isset($request->envComUrl)) {
            echo "<script>alert('图片没有上传！');history.go(-1);</script>"; exit;
        }
        $resFile = Upload::uploadImg($request,'envComUrl');
        if ($resFile['code']!=0) {
            echo "<script>alert('上传错误！');history.go(-1);</script>"; exit;
        }
        $imgUrl = $resFile['filePath'];
        $model = $this->model->getAdminOptionByUid($uid,$cate);
        $model['comEnvs'][$envKey][] = $imgUrl;
        $lastId = $this->model
            ->setAdminOptionByUid($uid,$model,$cate);
        return redirect($prefix.'#comEnv');
    }
}